from osgeo import ogr

ogr.UseExceptions()

driver: ogr.Driver = ogr.GetDriverByName("OpenFileGDB")

gdb_path = "D:/Code/Python/pyGDAL/Include/dataset/4gdal.gdb"
try:
    gdb:ogr.DataSource = driver.Open(gdb_path)
    print("gdb is avaliable!")
except Exception as E:
    print(E)

layer: ogr.Layer = gdb.GetLayer(0)
layer.SetAttributeFilter("SHAPE_Length > 0.1")
# layer.ResetReading()
# 遍历feature
# for index in range(layer.GetFeatureCount()):
#     feature:ogr.Feature = layer.GetNextFeature()
#     geometry:ogr.Geometry = feature.GetGeometryRef()
#     print(geometry.GetBoundary())

# 属性值过滤
print("按照属性值过滤")
for feature in layer:
    geometry:ogr.Geometry = feature.GetGeometryRef()
    print(feature.GetField("SHAPE_Length"))

# 空间拓扑过滤
print("按照空间拓扑值过滤")
wkt = "POLYGON ((119.434030 35.415658,119.527854 35.410418,119.517170 35.350255,119.439362 35.359535, 119.434030 35.415658))"
layer.SetSpatialFilter(ogr.CreateGeometryFromWkt(wkt))
for feature in layer:
    print(feature.GetField("SHAPE_Length"))
